const stateData = {
  visitedViews: [],
  cachedViews: [],
  queuePages: {}, // iframe 队列标识
};

const mutations = {
  /**
   * 添加访问过的视图到状态中
   * 此方法用于跟踪用户已访问的页面视图，以便在页面切换时提供面包屑导航等功能
   * @param {Object} state - 当前的应用状态对象
   * @param {Object} view - 当前访问的视图对象，包含视图的相关信息如路径、标题等
   */
  ADD_VISITED_VIEW: (state, view) => {
    // 输出当前视图信息以便调试
    console.log("view----------------", view);

    // 创建一个访问过的视图对象，合并当前视图信息和自定义的标题
    const visitedView = Object.assign({}, view, {
      title: view.query?.tag_title_value || view.meta.title || "no-name",
    });

    // 检查是否已经存在相同路径的访问视图
    const existingViewIndex = state.visitedViews.findIndex(
      (v) => v.path === view.path
    );

    // 如果是主页且已存在，则替换已存在的视图信息
    if (view.path === "/index" && existingViewIndex !== -1) {
      state.visitedViews[existingViewIndex] = visitedView;
      return false;
    }

    // 如果已访问视图中已存在该视图（根据完整路径判断），则不执行后续操作
    if (state.visitedViews.some((v) => v.fullPath === view.fullPath)) {
      return false;
    }

    // 将新的访问视图添加到已访问视图列表中
    state.visitedViews.push(visitedView);
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.fullPath)) {
      return false;
    }
    if (!view.meta.noCache) {
      state.cachedViews.push(view.fullPath);
    }
  },

  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.fullPath === view.fullPath) {
        state.visitedViews.splice(i, 1);
        break;
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name) {
        if (
          !state.visitedViews.some(
            (v) =>
              v.path === i &&
              JSON.stringify({ ...view.query }) !==
                JSON.stringify({ ...v.query })
          )
        ) {
          const index = state.cachedViews.indexOf(i);
          state.cachedViews.splice(index, 1);
          break;
        }
      }
    }
  },

  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.visitedViews = state.visitedViews.filter((v) => {
      return v.meta.affix || v.path === view.path;
    });
  },
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name) {
        const index = state.cachedViews.indexOf(i);
        state.cachedViews = state.cachedViews.slice(index, index + 1);
        break;
      }
    }
  },

  DEL_ALL_VISITED_VIEWS: (state) => {
    // keep affix tags
    let findObj = state.visitedViews.find((tag) => tag.meta.affix);
    state.visitedViews = findObj ? [findObj] : [];
  },
  DEL_ALL_CACHED_VIEWS: (state) => {
    state.cachedViews = [];
  },

  UPDATE_VISITED_VIEW: (state, view) => {
    for (let i = 0; i < state.visitedViews.length; i++) {
      let v = state.visitedViews[i];
      if (v.fullPath === view.fullPath) {
        v = Object.assign(v, view);
        break;
      }
    }
  },
  // iframe 数据
  SAVE_IFRAME_ROUTE: (state, data) => {
    state.queuePages = Object.assign(state.queuePages, data);
  },
};

const actions = {
  addView({ dispatch }, view) {
    dispatch("addVisitedView", view);
    dispatch("addCachedView", view);
  },
  addVisitedView({ commit }, view) {
    commit("ADD_VISITED_VIEW", view);
  },
  addCachedView({ commit }, view) {
    commit("ADD_CACHED_VIEW", view);
  },

  delView({ dispatch, state }, view) {
    return new Promise((resolve) => {
      dispatch("delVisitedView", view);
      dispatch("delCachedView", view);
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews],
      });
    });
  },
  delVisitedView({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_VISITED_VIEW", view);
      resolve([...state.visitedViews]);
    });
  },
  delCachedView({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_CACHED_VIEW", view);
      resolve([...state.cachedViews]);
    });
  },

  delOthersViews({ dispatch, state }, view) {
    return new Promise((resolve) => {
      dispatch("delOthersVisitedViews", view);
      dispatch("delOthersCachedViews", view);
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews],
      });
    });
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_OTHERS_VISITED_VIEWS", view);
      resolve([...state.visitedViews]);
    });
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise((resolve) => {
      commit("DEL_OTHERS_CACHED_VIEWS", view);
      resolve([...state.cachedViews]);
    });
  },

  delAllViews({ dispatch, state }, view) {
    return new Promise((resolve) => {
      dispatch("delAllVisitedViews", view);
      dispatch("delAllCachedViews", view);
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews],
      });
    });
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise((resolve) => {
      commit("DEL_ALL_VISITED_VIEWS");
      resolve([...state.visitedViews]);
    });
  },
  delAllCachedViews({ commit, state }) {
    return new Promise((resolve) => {
      commit("DEL_ALL_CACHED_VIEWS");
      resolve([...state.cachedViews]);
    });
  },
  updateVisitedView({ commit }, view) {
    commit("UPDATE_VISITED_VIEW", view);
  },
  // 增加iframe数据保存
  saveIframeRoute({ commit }, data) {
    commit("SAVE_IFRAME_ROUTE", data);
  },
};

export default {
  namespaced: true,
  state: stateData,
  mutations,
  actions,
};
